// // import Card from "../../../../Script/Game/Card";

import { _decorator, Component, misc, Node, Tween, tween, UITransform, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

import { YangLeGeYang_GameData } from "../Data/YangLeGeYang_GameData";
import YangLeGeYang_Card from "../Game/YangLeGeYang_Card";
import YangLeGeYang_CardManager from "../Manager/YangLeGeYang_CardManager";

@ccclass('YangLeGeYangOperatePanel')
export default class YangLeGeYang_OperatePanel extends Component {
        public static Instance: YangLeGeYang_OperatePanel;
        @property(Node)
        public EmptyLand: Node | null = null;
        onLoad() {
                YangLeGeYang_OperatePanel.Instance = this;
                this.ShowOn();
        }
        ShowOn() {
                this.node.active = true;
        }
        RefreshAllCard() {
                // console.log(`RefreshAllCard`);

                let count = 0;
                // console.log(`count`, count);
                for (let index = 0; index < YangLeGeYang_GameData.MapData_Operate.length; index++) {
                        const element = YangLeGeYang_GameData.MapData_Operate[index];
                        // console.log(`element`, element);

                        let index_MapData_Operate = index;
                        const card: YangLeGeYang_Card = YangLeGeYang_CardManager.Instance.CardList[element.list_index];

                        // let worldPos = card.node.convertToWorldSpaceAR(Vec3.ZERO);
                        // card.node.setParent(YangLeGeYang_OperatePanel.Instance.node);
                        // let dstPos = card.node.parent.convertToNodeSpaceAR(worldPos);
                        // 获取节点的世界坐标
                        let worldPos = card.node.getComponent(UITransform)!.convertToWorldSpaceAR(Vec3.ZERO);
                        // 先改父节点
                        card.node.setParent(YangLeGeYang_OperatePanel.Instance.node);
                        // 将世界坐标转为新父节点的本地坐标
                        let dstPos = card.node.parent!.getComponent(UITransform)!.convertToNodeSpaceAR(worldPos);


                        card.node.setPosition(dstPos);

                        let width_cell: number = YangLeGeYang_GameData.CellWidth; // ;
                        let width = width_cell * 7;// OperatePanel.Instance.node.width - 50;
                        const width_half: number = width / 2;

                        let x_start = -width_half + width_cell / 2 + width_cell * index_MapData_Operate;
                        // card.node.setPosition(v2(x_start, 0));

                        let y = 2// 10;
                        let pos = v3(x_start, y);

                        // card.node.angle = 20;

                        // console.log(`element.list_index`, element.list_index, YangLeGeYang_GameData.LastClickCardIndex)
                        // console.log(`card.cardGameState`, element.cardGameState)


                        // // const distance = card.node.position.sub(pos).mag();
                        // const distance = Vec3.distance(card.node.position, pos);


                        // 获取节点位置（本地坐标系）
                        const nodePos = card.node.getPosition();
                        // 或者如果你要全局坐标就用 card.node.worldPosition
                        const distance = Vec3.subtract(new Vec3(), nodePos, pos).length();

                        // console.log(distance);


                        let 是否是最近点击 = element.list_index === YangLeGeYang_GameData.LastClickCardIndex;
                        let duration = 0;
                        // if (是否是最近点击) {
                        //     duration = 0.0004 * distance;
                        // }
                        // else {
                        //     duration = 0.02;
                        // }

                        // if (duration < 0.1) {
                        //     duration = 0.1;
                        // }

                        duration = 0.12 + 0.00014 * distance;


                        if (是否是最近点击) {
                                const pos1 = card.node.position;
                                const pos2 = pos;

                                // 计算两点之间的差值向量
                                const dx = pos2.x - pos1.x;
                                const dy = pos2.y - pos1.y;

                                // 使用Math.atan2计算弧度，然后转换为角度
                                const angleRad = Math.atan2(dy, dx);
                                const angleDeg = misc.radiansToDegrees(angleRad);

                                // 如果需要0-360度的角度
                                const normalizedAngle = (angleDeg + 360) % 360;
                                card.main.angle = normalizedAngle + 90
                        }

                        tween(card.node)
                                .to(duration, { scale: v3(1, 1, 1) })
                                .start()

                        tween(card.node)
                                // .to(duration, { x: pos.x, y: pos.y, scaleX: 1, scaleY: 1 })
                                .to(duration, { position: v3(pos.x, pos.y) })
                                .call(() => {
                                        if (是否是最近点击) {
                                                const stgOg_angle = 0;
                                                const duration_2 = 0.05;
                                                let target = card.main;
                                                let 幅度 = 1;
                                                let 幅度_scale = 15;
                                                target.angle = 0
                                                Tween.stopAllByTarget(target);
                                                tween(target)
                                                        .to(duration_2, { angle: stgOg_angle + -幅度 * 幅度_scale })
                                                        .to(duration_2, { angle: stgOg_angle })
                                                        .to(duration_2, { angle: stgOg_angle + +幅度 * 幅度_scale })
                                                        .to(duration_2, { angle: stgOg_angle })
                                                        .start();
                                        }

                                })
                                .call(() => {
                                        card.node.zIndex = 0;

                                        count++;
                                        if (count >= YangLeGeYang_GameData.MapData_Operate.length) {
                                                // console.log(`一共几个结束`, count);
                                                let isMatch3 = YangLeGeYang_CardManager.XiaoChu3();
                                                // console.log(`isMatch3------`, isMatch3);

                                                if (isMatch3) {
                                                        // console.log(`isMatch3------`);

                                                        YangLeGeYang_OperatePanel.Instance.RefreshAllCard();
                                                }
                                        }
                                })
                                .start();


                        // else {
                        //     tween(card.node)
                        //         .to(0.2, { x: pos.x, y: pos.y, scaleX: 1, scaleY: 1 })
                        //         .call(() => {
                        //             card.node.zIndex = 0;

                        //             count++;
                        //             if (count >= YangLeGeYang_GameData.MapData_Operate.length) {
                        //                 // console.log(`一共几个结束`, count);
                        //                 let isMatch3 = YangLeGeYang_CardManager.XiaoChu3();
                        //                 // console.log(`isMatch3------`, isMatch3);

                        //                 if (isMatch3) {
                        //                     // console.log(`isMatch3------`);

                        //                     YangLeGeYang_OperatePanel.Instance.RefreshAllCard();
                        //                 }
                        //             }
                        //         })
                        //         .start();
                        // }


                }
        }
}